package com.token.iot.bratterycharging.boss.web;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.outstanding.framework.base.TransactionLogger;
import com.outstanding.framework.container.springmvc.WebContext;
import com.outstanding.framework.core.PendingException;
import com.outstanding.framework.core.ReturnCode;
import com.token.base.order.api.PayItemDTO;
import com.token.iot.bratterycharging.boss.web.ReqPageDto;
import com.token.iot.bratterycharging.boss.service.AgentsService;
import com.token.iot.bratterycharging.boss.service.DeviceService;
import com.token.iot.bratterycharging.boss.service.MerchantService;
import com.token.iot.bratterycharging.boss.service.OrderServcice;

@Controller
@RequestMapping("/agent")
public class AgentController {

	@Autowired
	private MerchantService merchantService;

	@Autowired
	private AgentsService agentService;
	
	@ResponseBody
	@RequestMapping(value = "/login.do")
	public Agents login(@RequestBody Agents dto) throws PendingException {
		TransactionLogger.debug("staring login " + String.valueOf(dto == null) + " ");
		// dto.setRole("管理员");
		Agents agent = agentService.selectByUserNamePassword(dto.getCode(), dto.getPassword());
		if (agent == null) {
			throw new PendingException(ResCode.LoginError);
		}
		WebContext.getSession().setAttribute("_agentid",  String.valueOf(agent.getId()));
		WebContext.getSession().setAttribute("_agent", agent);
		return agent;
	}

	/**
	 * 添加代理商
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/add.do")
	public String add(@RequestBody Agents dto) throws PendingException {
		TransactionLogger.debug("staring agent add " + String.valueOf(dto == null) + " ");
		Long operid = (Long) WebContext.getSession().getAttribute("_operid");
		if (operid == null) {
			throw new PendingException(ResCode.OperNotLogin);
		}
		return agentService.saveNotNull(dto, operid);
	}

	/**
	 * 代理商详情
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/detail.do")
	public Agents detail(String agentId) throws PendingException {
		TransactionLogger.debug("staring agent detail  ");
		Long operid = (Long) WebContext.getSession().getAttribute("_operid");
		String aid = agentId;
		if (operid == null) {
			String agentid = (String) WebContext.getSession().getAttribute("_agentid");
			if (agentid == null) {
				throw new PendingException(ResCode.OperNotLogin);
			}else {
				aid=agentid;
			}
		}
		// 如果operid 不为空，则需要传入 agentid

		return agentService.detail(aid);
	}
	/**
	 * 列出所有代理商
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/listall.do")
	public List<Agents> listall() throws PendingException {
		Long operid = (Long) WebContext.getSession().getAttribute("_operid");
		if (operid == null) {
			throw new PendingException(ResCode.OperNotLogin);

		}
		return agentService.listall();
	}

	/**
	 * 列出所有代理商
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/list.do")
	public PageInfo list(@RequestBody ReqPageDto dto) throws PendingException {
		Long operid = (Long) WebContext.getSession().getAttribute("_operid");
		if (operid == null) {
			throw new PendingException(ResCode.OperNotLogin);

		}
		return agentService.list(dto.getPageNo(),dto.getPageSize());
	}
	

	

	

	
	/**
	 * 结算报表查询
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/settle/query.do")
	public String settleQuery(@RequestBody Merchant dto) throws PendingException {
		TransactionLogger.debug("staring agent add " + String.valueOf(dto == null) + " ");

		Long operid = 0l;
		return merchantService.addMerchant(dto, operid);
	}

	/**
	 * 提现申请
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/cash/request.do")
	public String requestCash(@RequestBody Merchant dto) throws PendingException {
		TransactionLogger.debug("staring agent add " + String.valueOf(dto == null) + " ");

		Long operid = 0l;
		return merchantService.addMerchant(dto, operid);
	}

}
